package servlet;

import dao.EquipmentDao;
import dao.EquipmentDaoImpl;
import dao.TableFixDaoImpl;
import dao.FixDaoImpl;
import utils.string.session.SessionStringUtil;
import vo.User;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;

public class DeleteFixServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        int fixId = Integer.parseInt(req.getParameter("fixId"));
        int equip_id = Integer.parseInt(req.getParameter("equip_id"));
        int equip_num = Integer.parseInt(req.getParameter("equip_num"));
        FixDaoImpl fixDao = new FixDaoImpl();
        //在设备表中的数据也得需修改
        EquipmentDaoImpl equipmentDao = new EquipmentDaoImpl();
        equipmentDao.changeEquipment(equip_id,equip_num, EquipmentDao.ADD_EQUIPMENT_NUM);
        Boolean flag = fixDao.deleteByID(fixId);
        if(flag){
            jumpToJsp(req,resp);
        }
    }

    private void jumpToJsp(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        User user = (User) req.getSession().getAttribute(SessionStringUtil.USER);
        TableFixDaoImpl tableFixDao = new TableFixDaoImpl();
        HashMap fixs = tableFixDao.selectAll();
        req.getSession().setAttribute(SessionStringUtil.ALL_FIXS,fixs);
        if(user.getAuth_id() == 1){
            resp.sendRedirect("/equipmentManager_war_explode/manageAllFix.jsp");//跳转到管理者的Using界面
        }else{
            resp.sendRedirect("/equipmentManager_war_explode/manageUserFix.jsp");//普通用户的界面
        }
    }

}
